package com.lihua.project.system.article.service.impl;

import java.util.Date;
import java.util.List;

import com.lihua.common.utils.SystemCountUtils;
import com.lihua.project.system.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.RedisSystemException;
import org.springframework.stereotype.Service;
import com.lihua.project.system.article.mapper.ArticleMapper;
import com.lihua.project.system.article.domain.Article;
import com.lihua.project.system.article.service.IArticleService;
import com.lihua.common.utils.text.Convert;

import static com.lihua.common.utils.security.ShiroUtils.getSysUser;

/**
 * 文章帖子Service业务层处理
 * 
 * @author lihua
 * @date 2022-03-27
 */
@Service
public class ArticleServiceImpl implements IArticleService 
{
    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private SystemCountUtils systemCountUtils;

    /**
     * 查询文章帖子
     * 
     * @param id 文章帖子主键
     * @return 文章帖子
     */
    @Override
    public Article selectArticleById(Long id)
    {
        return articleMapper.selectArticleById(id);
    }

    /**
     * 查询文章帖子列表
     * 
     * @param article 文章帖子
     * @return 文章帖子
     */
    @Override
    public List<Article> selectArticleList(Article article)
    {
        return articleMapper.selectArticleList(article);
    }

    /**
     * 新增文章帖子
     * 
     * @param article 文章帖子
     * @return 结果
     */
    @Override
    public int insertArticle(Article article) {
        article.setArticleNewstime(new Date());
        User user = getSysUser();
        article.setUserId(user.getUserId());

        try{
            //统计新增帖子
            systemCountUtils.addQuantity("article-count");
        }catch (RedisSystemException e){
            //异常重试
            systemCountUtils.addQuantity("article-count");
        }
        return articleMapper.insertArticle(article);
    }

    /**
     * 修改文章帖子
     * 
     * @param article 文章帖子
     * @return 结果
     */
    @Override
    public int updateArticle(Article article)
    {
        return articleMapper.updateArticle(article);
    }

    /**
     * 批量删除文章帖子
     * 
     * @param ids 需要删除的文章帖子主键
     * @return 结果
     */
    @Override
    public int deleteArticleByIds(String ids)
    {
        return articleMapper.deleteArticleByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除文章帖子信息
     * 
     * @param id 文章帖子主键
     * @return 结果
     */
    @Override
    public int deleteArticleById(Long id)
    {
        return articleMapper.deleteArticleById(id);
    }

    @Override
    public List<Article> selectArticleListByArticleTitle(String key) {
        return  articleMapper.selectArticleListByArticleTitle(key);
    }
}
